﻿//-----------------------------------------------------------------------
// <copyright file="RootTemplateWizard.cs" company="Adam Toth">
// Copyright © Adam Toth 2013
// </copyright>
//-----------------------------------------------------------------------
namespace ServiceAppStarterKit.TemplateWizard
{
    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using Microsoft.VisualStudio.TemplateWizard;
    using EnvDTE;

    /// <summary>
    /// The wizard that runs for the project group template.
    /// </summary>
    public class RootTemplateWizard : IWizard
    {
        /// <summary>
        /// A static global dictionary of parameters, accessible in child project wizards.
        /// </summary>
        internal static Dictionary<string, string> GlobalDictionary;

        /// <summary>
        /// A class for creating key files.
        /// </summary>
        internal static ProjectSigningManager SigningManager;

        /// <summary>
        /// Reference to the automation object if needed.
        /// </summary>
        private DTE _dte;

        /// <summary>
        /// Before opening file.
        /// </summary>
        /// <param name="projectItem">The project item.</param>
        public void BeforeOpeningFile(EnvDTE.ProjectItem projectItem)
        {
        }

        /// <summary>
        /// Project finished generating.
        /// </summary>
        /// <param name="project">The project.</param>
        public void ProjectFinishedGenerating(EnvDTE.Project project)
        {
        }

        /// <summary>
        /// Project item finished generating.
        /// </summary>
        /// <param name="projectItem">The project item.</param>
        public void ProjectItemFinishedGenerating(EnvDTE.ProjectItem projectItem)
        {
        }

        /// <summary>
        /// Run finished.
        /// </summary>
        public void RunFinished()
        {
        }

        /// <summary>
        /// Run started.
        /// </summary>
        /// <param name="automationObject">Automation object.</param>
        /// <param name="replacementsDictionary">The current replacements dictionary.</param>
        /// <param name="runKind">The kind of run.</param>
        /// <param name="customParams">Custom parameters.</param>
        public void RunStarted(object automationObject, Dictionary<string, string> replacementsDictionary, WizardRunKind runKind, object[] customParams)
        {
            _dte = automationObject as DTE;

            if (replacementsDictionary == null)
            {
                throw new ArgumentNullException("replacementsDictionary");
            }

            SigningManager = new ProjectSigningManager();
            SigningManager.GenerateKeyFile();

            GlobalDictionary = new Dictionary<string, string>();
            GlobalDictionary.Add("$originalprojectname$", replacementsDictionary["$safeprojectname$"]);
            GlobalDictionary.Add("$originalprojectnameunsafe$", replacementsDictionary["$projectname$"]);
            GlobalDictionary.Add("$servicefeatureguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$servicefeatureitemguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$servicepackageguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$clientpackageguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$webclientsspdata$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$webservicesspdata$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$isapispdata$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$administrationactionsspdata$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$sqlspdata$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$resourcesspdata$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$adminspdata$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$adminresourcesspdata$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$serviceguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$serviceproxyguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$serviceinstanceguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$serviceapplicationguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$serviceapplicationproxyguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$databaseguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$wcfserviceguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$servicehostfactoryguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$servicecsprojguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$clientcsprojguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$administrationactionscustomactionid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$serviceapplicationclassguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$includedb$", replacementsDictionary["$includedb$"]);
            GlobalDictionary.Add("$customactiongroupid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$serviceadminfeaturereceiverguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$registrationspdata$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$newserviceapplicationpsguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$newserviceapplicationproxypsguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$removeservicepsguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$setserviceapplicationpsguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$setserviceapplicationproxypsguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$spversion$", replacementsDictionary["$spversion$"]);
            GlobalDictionary.Add("$restserviceguid$", Guid.NewGuid().ToString("D"));
            GlobalDictionary.Add("$iwcfserviceguid$", Guid.NewGuid().ToString("D"));
        }

        /// <summary>
        /// Should add project item.
        /// </summary>
        /// <param name="filePath">The path of the file.</param>
        /// <returns>True if the file should be added to the project.</returns>
        public bool ShouldAddProjectItem(string filePath)
        {
            return true;
        }

        /// <summary>
        /// Moves a project folder to a different location.
        /// </summary>
        /// <param name="projectFullName"></param>
        private void MoveProject(Project project, string solutionRoot)
        {
            string projectName = Path.GetFileName(project.FullName);
            DirectoryInfo oldProjectDir = Directory.GetParent(project.FullName);
            string newProjectDir = Path.Combine(solutionRoot, oldProjectDir.Name);

            _dte.Solution.Remove(project);
            Directory.Move(oldProjectDir.FullName, newProjectDir);
            _dte.Solution.AddFromFile(Path.Combine(newProjectDir, projectName));
        }
    }
}